package hu.fic.weddingstorefinder.activity;

import java.util.ArrayList;

import hu.fic.weddingstorefinder.R;
import hu.fic.weddingstorefinder.db.WeddingStoreDBAdapter;
import hu.fic.weddingstorefinder.db.table.Dress;
import hu.fic.weddingstorefinder.listadapter.DressListAdapter;
import hu.fic.weddingstorefinder.listadapter.DressListSectionHeader;
import hu.fic.weddingstorefinder.listadapter.ListItem;
import hu.fic.weddingstorefinder.parcel.DressParcelable;
import hu.fic.weddingstorefinder.parcel.StoreParcelable;
import hu.fic.weddingstorefinder.utils.Constants;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class StoreDetailsActivity extends Activity implements OnItemClickListener {
	private ArrayList<ListItem> dressItemList = new ArrayList<ListItem>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.storedetails_layout);
		final StoreParcelable storeParcel = this.getIntent().getParcelableExtra(Constants.POI_PARCEL_KEY);
		this.setTitle(storeParcel.getPoiName());
		
		TextView storeNameTV = (TextView)findViewById(R.id.storedetails_storename_label);
		storeNameTV.setText(storeParcel.getPoiName());
		
		TextView addressTV = (TextView)findViewById(R.id.storedetails_address_label);
		addressTV.setText(storeParcel.getAddress());
		
		TextView cityTV = (TextView)findViewById(R.id.storedetails_ciy_label);
		cityTV.setText(storeParcel.getCity());
		
		TextView zipTV = (TextView)findViewById(R.id.storedetails_zip_label);
		zipTV.setText(storeParcel.getZipCode());
		
		Button callStoreButton = (Button) findViewById(R.id.storedetails_callstore_button);
		callStoreButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					
					Intent callIntent = new Intent(Intent.ACTION_CALL);
					callIntent.setData(Uri.parse("tel:"
							+ storeParcel.getPhone()));
					startActivity(callIntent);
				} catch (ActivityNotFoundException e) {
					e.printStackTrace();
				}
			}
		});
		
		//show map button config:
		Button showMapButton = (Button) findViewById(R.id.storedetails_showmap_button);
		showMapButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					
					Intent myIntent = new Intent(StoreDetailsActivity.this, StoreMapActivity.class);
					double[]storeCoordinates = {storeParcel.getLongitude(), storeParcel.getLatitude()};
					myIntent.putExtra(Constants.COORDINATES_TO_SHOW_KEY, storeCoordinates);
			        myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			        startActivity(myIntent);
				} catch (ActivityNotFoundException e) {
					e.printStackTrace();
				}
			}
		});
		
		//load dresses:
		ListView dressListView = (ListView)findViewById(R.id.storedetails_dresslistview);
		try {
			Dress dress = new Dress(WeddingStoreDBAdapter.getInstance().getDb(), this);
			dress.setCustomPOIId(storeParcel.getCustomPoiId());
			dress.load();
			DressListSectionHeader sectionHeader = new DressListSectionHeader(getString(R.string.dressList));
			dressItemList.add(sectionHeader);
			while (dress.nextRow()) {
				DressParcelable dressParcel = new DressParcelable(dress);
				dressItemList.add(dressParcel);
			}
			DressListAdapter dressAdapter = new DressListAdapter(dressItemList);
			dressListView.setAdapter(dressAdapter);
			dressListView.setOnItemClickListener(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		try {
			DressParcelable dressParcelable = (DressParcelable)dressItemList.get(position);
			Intent intent = new Intent(this, DressDetailsActivity.class);
			intent.putExtra(Constants.DRESS_PARCEL_KEY, dressParcelable);
			this.startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
